<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>spring core annotations | haijd</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  <meta name="keywords" content="Spring Annotations" />
  
  
  
  
  <meta name="description" content="Spring 核心注解">
<meta name="keywords" content="Spring Annotations">
<meta property="og:type" content="article">
<meta property="og:title" content="Spring Core Annotations">
<meta property="og:url" content="http://www.hais2.com/2018/06/12/Spring-Core-Annotations-1/index.html">
<meta property="og:site_name" content="haijd">
<meta property="og:description" content="Spring 核心注解">
<meta property="og:locale" content="default">
<meta property="og:image" content="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1528780019766&di=68e5f2f2eef5b87bf5b5d54455fe994f&imgtype=jpg&src=http%3A%2F%2Fimg1.imgtn.bdimg.com%2Fit%2Fu%3D2161947716%2C2940818680%26fm%3D214%26gp%3D0.jpg">
<meta property="og:updated_time" content="2018-06-13T02:15:31.848Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Spring Core Annotations">
<meta name="twitter:description" content="Spring 核心注解">
<meta name="twitter:image" content="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1528780019766&di=68e5f2f2eef5b87bf5b5d54455fe994f&imgtype=jpg&src=http%3A%2F%2Fimg1.imgtn.bdimg.com%2Fit%2Fu%3D2161947716%2C2940818680%26fm%3D214%26gp%3D0.jpg">
  
    <link rel="alternate" href="/atom.xml" title="haijd" type="application/atom+xml">
  
  <link rel="icon" href="/css/images/favicon.ico">
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link href="https://fonts.googleapis.com/css?family=Open+Sans|Montserrat:700" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Roboto:400,300,300italic,400italic" rel="stylesheet" type="text/css">
  <link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">
  <style type="text/css">
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/9749f0/00000000000000000001008f/27/l?subset_id=2&fvd=n5) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/90cf9f/000000000000000000010091/27/l?subset_id=2&fvd=n7) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/8a5494/000000000000000000013365/27/l?subset_id=2&fvd=n4) format("woff2");font-weight:lighter;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/d337d8/000000000000000000010095/27/l?subset_id=2&fvd=i4) format("woff2");font-weight:400;font-style:italic;}</style>
    
  <link rel="stylesheet" id="athemes-headings-fonts-css" href="//fonts.googleapis.com/css?family=Yanone+Kaffeesatz%3A200%2C300%2C400%2C700&amp;ver=4.6.1" type="text/css" media="all">
  <link rel="stylesheet" href="/css/style.css">

  <script src="/js/jquery-3.1.1.min.js"></script>

  <!-- Bootstrap core CSS -->
  <link rel="stylesheet" href="/css/bootstrap.css" >
  <link rel="stylesheet" href="/css/hiero.css" >
  <link rel="stylesheet" href="/css/glyphs.css" >
  
    <link rel="stylesheet" href="/css/vdonate.css" >
  

</head>

<script>
var themeMenus = {};

  themeMenus["/"] = "Home"; 

  themeMenus["/archives"] = "Archives"; 

  themeMenus["/categories"] = "Categories"; 

  themeMenus["/tags"] = "Tags"; 

  themeMenus["/about"] = "About"; 

</script>


  <body data-spy="scroll" data-target="#toc" data-offset="50">


  <header id="allheader" class="site-header" role="banner">
  <div class="clearfix container">
      <div class="site-branding">

          <h1 class="site-title">
            
              <a href="/" title="haijd" rel="home"> haijd </a>
            
          </h1>

          
            <div class="site-description">Stay Hungry,Stay Foolish</div>
          
            
          <nav id="main-navigation" class="main-navigation" role="navigation">
            <a class="nav-open">Menu</a>
            <a class="nav-close">Close</a>
            <div class="clearfix sf-menu">

              <ul id="main-nav" class="nmenu sf-js-enabled">
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/">Home</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/archives">Archives</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/categories">Categories</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/tags">Tags</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/about">About</a> </li>
                    
              </ul>
            </div>
          </nav>


      </div>
  </div>
</header>




  <div id="container">
    <div id="wrap">
            
      <div id="content" class="outer">
        
          <section id="main" style="float:none;"><article id="post-Spring-Core-Annotations-1" style="width: 66%; float:left;" class="article article-type-post" itemscope itemprop="blogPost" >
  <div id="articleInner" class="clearfix post-1016 post type-post status-publish format-standard has-post-thumbnail hentry category-template-2 category-uncategorized tag-codex tag-edge-case tag-featured-image tag-image tag-template">
    
<div class="article-gallery">
  <div class="article-gallery-photos">
    
      <a class="article-gallery-img fancybox" href="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1528780019766&di=68e5f2f2eef5b87bf5b5d54455fe994f&imgtype=jpg&src=http%3A%2F%2Fimg1.imgtn.bdimg.com%2Fit%2Fu%3D2161947716%2C2940818680%26fm%3D214%26gp%3D0.jpg" rel="gallery_cjie4nakd006r3swalq3lu5ho">
        <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1528780019766&di=68e5f2f2eef5b87bf5b5d54455fe994f&imgtype=jpg&src=http%3A%2F%2Fimg1.imgtn.bdimg.com%2Fit%2Fu%3D2161947716%2C2940818680%26fm%3D214%26gp%3D0.jpg" itemprop="image">
      </a>
    
  </div>
</div>

    
      <header class="article-header">
        
  
    <h1 class="thumb" class="article-title" itemprop="name">
      Spring Core Annotations
    </h1>
  

      </header>
    
    <div class="article-meta">
      
	Posted on <a href="/2018/06/12/Spring-Core-Annotations-1/" class="article-date">
	  <time datetime="2018-06-12T02:15:12.000Z" itemprop="datePublished">June 12, 2018</time>
	</a>

      
	<span id="busuanzi_container_page_pv">
	  本文总阅读量<span id="busuanzi_value_page_pv"></span>次
	</span>

    </div>
    <div class="article-entry" itemprop="articleBody">
      
        <blockquote>
<p>原文地址：<a href="http://www.baeldung.com/spring-core-annotations" target="_blank" rel="noopener">http://www.baeldung.com/spring-core-annotations</a><br>此篇为译文，由于英语水平有限，可能对原文的理解有误，欢迎指正，不甚感激！</p>
</blockquote>
<!-- TOC -->
<ul>
<li><a href="#1">1. 概览</a></li>
<li><a href="#2-di">2. DI有关的注解</a><ul>
<li><a href="#21-autowired">2.1. <code>@Autowired</code></a></li>
<li><a href="#22-bean">2.2. <code>@Bean</code></a></li>
<li><a href="#23-qualifier">2.3. <code>@Qualifier</code></a></li>
<li><a href="#24-required">2.4. <code>@Required</code></a></li>
<li><a href="#25-value">2.5. <code>@Value</code></a></li>
<li><a href="#26-dependson">2.6. <code>@DependsOn</code></a></li>
<li><a href="#27-lazy">2.7. <code>@Lazy</code></a></li>
<li><a href="#28-lookup">2.8. <code>@Lookup</code></a></li>
<li><a href="#29-primary">2.9. <code>@Primary</code></a></li>
<li><a href="#210-scope">2.10. <code>@Scope</code></a></li>
</ul>
</li>
<li><a href="#3">3. 上下文配置注解</a><ul>
<li><a href="#31-profile">3.1. <code>@Profile</code></a></li>
<li><a href="#32-import">3.2. <code>@Import</code></a></li>
<li><a href="#33-importresource">3.3. <code>@ImportResource</code></a></li>
<li><a href="#34-propertysource">3.4. <code>@PropertySource</code></a></li>
<li><a href="#35-propertysources">3.5. <code>@PropertySources</code></a></li>
</ul>
</li>
<li><a href="#4">4. 总结</a></li>
</ul>
<!-- /TOC -->
<h1 id="1-概览"><a href="#1-概览" class="headerlink" title="1. 概览"></a>1. 概览</h1><p>我们可以利用<code>org.springframework.beans.factory.annotation</code>和<code>org.springframework.context.annotation</code>这两个包提供的注解功能使用Spring的依赖注入。<br>我们经常称它为”Spring core annotations”，我将在本教程对此进行回顾。</p>
<h1 id="2-DI有关的注解"><a href="#2-DI有关的注解" class="headerlink" title="2. DI有关的注解"></a>2. DI有关的注解</h1><h2 id="2-1-Autowired"><a href="#2-1-Autowired" class="headerlink" title="2.1. @Autowired"></a>2.1. <code>@Autowired</code></h2><p>我们可以使用<code>@Autowired</code>注解标记需要Spring去解析和注入的依赖项。<a id="more"></a></p>
<p>构造函数注入：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span></span>&#123;</span><br><span class="line">    Engine engine;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    Car(Engine engine)&#123;</span><br><span class="line">        <span class="keyword">this</span>.engine=engine;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>Setter注入：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span></span>&#123;</span><br><span class="line">    Engine engine;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">setEngine</span><span class="params">(Engine engine)</span></span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.engine=engine;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>字段注入：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span></span>&#123;</span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    Engine engine;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><code>@Autowired</code>注解有一个布尔类型的名为<code>required</code>的参数，默认值为<code>true</code>。它可以调整Spring在没有找到所需的bean时的行为，当值为<code>true</code>时，会抛出异常，值为<code>false</code>时，则什么都不会发生，只不过在调用“注入”的变量时，可能会得到一个<code>null</code>引用。<br>Note:如果使用的时构造函数注入，那么所有构造函数的参数都是必须的。<br>从Spring4.3版本开始，我们可以直接省略<code>@Autowired</code>注解，就可以实现依赖的注入，除非我们声明了两个或以上的构造函数，这时才需要指定一个构造函数用<code>@Autowired</code>修饰。<br>更多的信息可以阅读我们的其他文章<a href="http://www.baeldung.com/spring-autowire" target="_blank" rel="noopener">@Autowired</a>和<a href="http://www.baeldung.com/constructor-injection-in-spring" target="_blank" rel="noopener">constructor injection</a></p>
<h2 id="2-2-Bean"><a href="#2-2-Bean" class="headerlink" title="2.2. @Bean"></a>2.2. <code>@Bean</code></h2><p><code>@Bean</code>标记一个实例化Spring bean的工厂方法：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Bean</span></span><br><span class="line"><span class="function">Engine <span class="title">engine</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> Engine();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>当需要返回类型的新实例时，Spring会调用这些方法。<br>返回的bean的name和工厂方法的名称一致，如果想要不同的名称，可以在<code>@Bean</code>注解中传入一个<code>String</code>类型的值，这个值会作为返回的bean的name：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Bean</span>(<span class="string">"engine"</span>)</span><br><span class="line"><span class="function">Engine <span class="title">getEngine</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> Engine();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>Note:用<code>@Bean</code>注解标记的所有方法，必须在<code>@Configuration</code>标记的类中。</p>
<h2 id="2-3-Qualifier"><a href="#2-3-Qualifier" class="headerlink" title="2.3. @Qualifier"></a>2.3. <code>@Qualifier</code></h2><p>我们使用<code>@Qualifier</code>与<code>@Autowired</code>注解，在一些使用不明确的情况下，提供所需的<code>bean name</code>或者<code>bean id</code>。<br>举个例子，下面的两个bean实现了同一个接口：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Bike</span> <span class="keyword">implements</span> <span class="title">Vehicle</span></span>&#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span> <span class="keyword">implements</span> <span class="title">Vehicle</span></span>&#123;&#125;</span><br></pre></td></tr></table></figure></p>
<p>如果Spring需要注入一个<code>Vehicle</code> bean，但是<code>Vehicle</code>有多个定义(实现)，在这个案例中，我们可以使用<code>@Qualifier</code>注解显示的提供一个<code>bean name</code>。</p>
<p>使用构造函数注入：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Autowired</span></span><br><span class="line">Biker(<span class="meta">@Qualifier</span>(<span class="string">"bike"</span>) Vehicle vehicle)&#123;</span><br><span class="line">    <span class="keyword">this</span>.vehicle = vehicle;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>使用setter注入：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Autowired</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">setVihicle</span><span class="params">(@Qualifier(<span class="string">"bike"</span>)</span> Vehicle vehicle)</span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.vehicle = vehicle;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>或者：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Autowired</span></span><br><span class="line"><span class="meta">@Qualifier</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">setVihicle</span><span class="params">(Vehicle vehicle)</span></span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.vehicle = vehicle;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>使用字段注入：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Autowired</span></span><br><span class="line"><span class="meta">@Qualifier</span>(<span class="string">"bike"</span>)</span><br><span class="line">Vehicle vehicle;</span><br></pre></td></tr></table></figure></p>
<p>更多的信息，请阅读<a href="http://www.baeldung.com/spring-autowire" target="_blank" rel="noopener">这篇文章</a></p>
<h2 id="2-4-Required"><a href="#2-4-Required" class="headerlink" title="2.4. @Required"></a>2.4. <code>@Required</code></h2><p><code>@Required</code>注解用来在<code>setter</code>方法上标记我们想通过XML填入的依赖：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Required</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">setColor</span><span class="params">(String color)</span></span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.color = color;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">class</span>=<span class="string">"com.baeldung.annotations.Bike"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"color"</span> <span class="attr">value</span>=<span class="string">"green"</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>否则，会抛出<code>BeanInitializationException</code>异常。</p>
<h2 id="2-5-Value"><a href="#2-5-Value" class="headerlink" title="2.5. @Value"></a>2.5. <code>@Value</code></h2><p>我们可以使用<code>@Value</code>注解将属性值注入到bean中，<code>@Value</code>注解兼容构造函数注入、setter注入、字段注入。</p>
<p>构造函数注入：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Engine(<span class="meta">@Value</span>(<span class="string">"8"</span>) <span class="keyword">int</span> cyLinderCount)&#123;</span><br><span class="line">    <span class="keyword">this</span>.cyLinderCount = cyLinderCount;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>setter注入：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Autowired</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">setCyLinderCount</span><span class="params">(@Value(<span class="string">"8"</span>)</span> <span class="keyword">int</span> cyLinderCount)</span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.cyLinderCount = cyLinderCount; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>或者：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Value</span>(<span class="string">"8"</span>)</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">setCyLinderCount</span><span class="params">(<span class="keyword">int</span> cyLinderCount)</span></span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.cyLinderCount = cyLinderCount; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>字段注入：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Value</span>(<span class="string">"8"</span>)</span><br><span class="line"><span class="keyword">int</span> cylinderCount;</span><br></pre></td></tr></table></figure></p>
<p>当然了，注入静态值是没有用的，不过我们可以在<code>@Value</code>中使用“占位字符串”的方式，写入一个外部定义的源数据。例如，在<code>.properties</code>或者<code>.yaml</code>文件。<br>假设有以下的<code>.properties</code>文件：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">engine.fuelType=petrol</span><br></pre></td></tr></table></figure></p>
<p>我们可以将engine.fuelType的值注入：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Value</span>(<span class="string">"$&#123;engine.fuelType&#125;"</span>)</span><br><span class="line">String fuelType;</span><br></pre></td></tr></table></figure></p>
<p>我们还可以使用<code>@Value</code>的<code>SpEL</code>表达式，关于更多的高级用法，可以阅读我们<a href="http://www.baeldung.com/spring-value-annotation" target="_blank" rel="noopener">关于@Value的文章</a>。</p>
<h2 id="2-6-DependsOn"><a href="#2-6-DependsOn" class="headerlink" title="2.6. @DependsOn"></a>2.6. <code>@DependsOn</code></h2><p>我们可以使用<code>@DependsOn</code>注解标注Spring在初始化当前(被@DependsOn标记)的bean之前，先初始化其他bean。通常情况下，这个依赖注入的关系是自动完成的，Spring会查找bean之间的显式的依赖关系自动初始化。<br>我们仅仅需要在初始化隐式依赖关系bean的时候使用<code>@DependsOn</code>注解，例如JDBC驱动加载或者静态变量初始化(静态变量会先于类加载，所以需要提前初始化其他的bean，博主注)。<br>我们可以使用<code>@DependsOn</code>指定一个当前bean依赖的另一个bean的名字，<code>@DependsOn</code>注解的参数需要一个String类型的数组(或者String字符串)：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@DependsOn</span>(<span class="string">"engine"</span>)</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span> <span class="keyword">implements</span> <span class="title">Vehicle</span> </span>&#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@DependsOn</span>(&#123;<span class="string">"engine"</span>&#125;)</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span> <span class="keyword">implements</span> <span class="title">Vehicle</span> </span>&#123;&#125;</span><br></pre></td></tr></table></figure></p>
<p>另外的，如果我们使用<code>@Bean</code>注解定义一个bean，那么这个工厂方法应该使用<code>@DependsOn</code>注解：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Bean</span></span><br><span class="line"><span class="meta">@DependsOn</span>(<span class="string">"fuel"</span>)</span><br><span class="line"><span class="function">Engine <span class="title">engine</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> Engine();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="2-7-Lazy"><a href="#2-7-Lazy" class="headerlink" title="2.7. @Lazy"></a>2.7. <code>@Lazy</code></h2><p>我们使用<code>@Lazy</code>注解标记我们想要延迟初始化的bean，默认情况，Spring会在<code>application context</code> startup/bootstrapping时第一时间创建单例bean。然而，有时我们不需要在<code>application</code> startup时创建bean。<br><code>@Lazy</code>注解的行为有些不同，取决与我们在什么位置使用它，我们可以在下列情况使用：</p>
<ul>
<li>用在<code>@Bean</code>标记的工厂方法上，会延迟该方法的调用(对应的，工厂方法中的bean也会延迟创建)。</li>
<li><code>@Configuration</code>标记的类和<code>@Bean</code>标记的方法都会收到影响。</li>
<li>用在<code>@Component</code>标记的类上，如果它不是一个<code>@Configuration</code>类，那么就会被延迟初始化。</li>
<li>用在<code>@Autowired</code>标记的构造函数、setter方法或者字段上面，可以延迟加载这个依赖。</li>
</ul>
<p><code>@Lazy</code>注解有一个bool类型的参数，默认值为true，这个参数会改变<code>@Lazy</code>注解的行为。<br>例如，在一个全局设置为懒加载时，第一时间加载；或者配置具体的<code>@Bean</code>工厂方法先于<code>@Configuration</code>类加载：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="meta">@Lazy</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">VehicleFactoryConfig</span> </span>&#123;</span><br><span class="line"> </span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="meta">@Lazy</span>(<span class="keyword">false</span>)</span><br><span class="line">    <span class="function">Engine <span class="title">engine</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> Engine();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>更近一步的阅读，请查阅<a href="http://www.baeldung.com/spring-lazy-annotation" target="_blank" rel="noopener">this article</a>。</p>
<h2 id="2-8-Lookup"><a href="#2-8-Lookup" class="headerlink" title="2.8. @Lookup"></a>2.8. <code>@Lookup</code></h2><p>用<code>@Lookup</code>注解的方法告诉Spring在我们调用它时返回方法返回类型的一个实例。<br>更多的信息请查看关于<code>Lookup</code>注解的文章<a href="http://www.baeldung.com/spring-lookup" target="_blank" rel="noopener">can be found in this article</a></p>
<h2 id="2-9-Primary"><a href="#2-9-Primary" class="headerlink" title="2.9. @Primary"></a>2.9. <code>@Primary</code></h2><p>有时我们需要定义同类型的多个bean，这时Spring的依赖注入时不会成功的，因为Spring并不知道我们需要注入哪一个bean。<br>我们可能已经知道该问题的解决选项：使用<code>@Qualifier</code>标记所有的连接点，并且指定bean的name。<br>然而大多数时候我们只需要一个具体的bean，并不需要其他的bean，这时我们可以使用<code>@Primary</code>注解简化这个案例：我们可以用<code>@Primary</code>标注那个最常使用的bean，当注入点没有使用<code>@Qualifier</code>指定注入bean名称时，默认选择这个bean：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="meta">@Primary</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span> <span class="keyword">implements</span> <span class="title">Vehicle</span> </span>&#123;&#125;</span><br><span class="line"> </span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Bike</span> <span class="keyword">implements</span> <span class="title">Vehicle</span> </span>&#123;&#125;</span><br><span class="line"> </span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Driver</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    Vehicle vehicle; <span class="comment">// 默认会注入@Primary标记的Car bean</span></span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Biker</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="meta">@Qualifier</span>(<span class="string">"bike"</span>)</span><br><span class="line">    Vehicle vehicle;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>在上面的例子中，<code>Car</code>是被<code>@Primary</code>标记的，因此在<code>Driver</code>类中，Spring会默认注入<code>Car</code> bean。当然了在<code>Biker</code> bean中<code>vehicle</code>变量会赋值一个<code>Bike</code>对象，因为它是被<code>@Qualifier</code>明确指定为<code>bike</code> name的bean。</p>
<h2 id="2-10-Scope"><a href="#2-10-Scope" class="headerlink" title="2.10. @Scope"></a>2.10. <code>@Scope</code></h2><p>我们可以使用<code>@Scope</code>注解定义<code>@Component</code>或者<code>Bean</code>的<a href="http://www.baeldung.com/spring-bean-scopes" target="_blank" rel="noopener">作用域</a>。作用域可以是<code>singleton</code>、<code>prototype</code>、<code>request</code>、<code>session</code>、<code>globalSession</code>中的一个，或者自定义的作用域。<br>举例：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="meta">@Scope</span>(<span class="string">"prototype"</span>)</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Engine</span> </span>&#123;&#125;</span><br></pre></td></tr></table></figure></p>
<h1 id="3-上下文配置注解"><a href="#3-上下文配置注解" class="headerlink" title="3. 上下文配置注解"></a>3. 上下文配置注解</h1><p>我们可以使用本节描述的内容来对应用上下文进行配置。</p>
<h2 id="3-1-Profile"><a href="#3-1-Profile" class="headerlink" title="3.1. @Profile"></a>3.1. <code>@Profile</code></h2><p>如果我们希望Spring仅在特定配置文件处于活动状态时才使用<code>@Component</code>类或<code>@Bean</code>方法，我们可以用<code>@Profile</code>来标记它，我们可以在<code>@Profile</code>注解的参数中传入配置文件的名称。<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="meta">@Profile</span>(<span class="string">"sportDay"</span>)</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Bike</span> <span class="keyword">implements</span> <span class="title">Vehicle</span> </span>&#123;&#125;</span><br></pre></td></tr></table></figure></p>
<p>你可以从<a href="http://www.baeldung.com/spring-profiles" target="_blank" rel="noopener">这篇文章</a>中获取更多关于<code>@Profile</code>的信息。</p>
<h2 id="3-2-Import"><a href="#3-2-Import" class="headerlink" title="3.2. @Import"></a>3.2. <code>@Import</code></h2><p>使用<code>@Import</code>注解可以使用具体的<code>@Configuration</code>类而不是使用组件扫描，我们可以提供一个<code>@Configuration</code>类给<code>@Import</code>作为参数：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Import</span>(VehiclePartSupplier.class)</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">VehicleFactoryConfig</span> </span>&#123;&#125;</span><br></pre></td></tr></table></figure></p>
<p>博主注：<code>@Import</code>注解其实就是把多个分散的<code>@Configuration</code>配置类给整合到一起，使用的时候用这个一个类就可以了，举个例子：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JavaConfigService</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Bean</span>(name=<span class="string">"userService"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Car <span class="title">getUerService</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> UserService();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JavaConfigModel</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Bean</span>(name=<span class="string">"user"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Car <span class="title">getUser</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> User();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//将两个配置合并起来</span></span><br><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="meta">@Import</span>(&#123;JavaConfigService.class,JavaConfigModel.class&#125;)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">GlobalConfig</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 其他的bean的定义</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span> <span class="params">(String args[])</span></span>&#123;</span><br><span class="line">    <span class="comment">// 在spring中就可以使用合并配置，而不用一个一个去实例化配置了</span></span><br><span class="line">    AnnotationConfigApplicationContext context = <span class="keyword">new</span> AnnotationConfigApplicationContext(GlobalConfig.class);</span><br><span class="line">    User user = (User)context.getBean(<span class="string">"user"</span>);</span><br><span class="line">    UserService us = (UserService)context.getBean(<span class="string">"userService"</span>);</span><br><span class="line">    context.close();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="3-3-ImportResource"><a href="#3-3-ImportResource" class="headerlink" title="3.3. @ImportResource"></a>3.3. <code>@ImportResource</code></h2><p>我们可以使用该注解导入指定的XML配置，可以使用具体的XML文件的本地路径、或者别名作为该注解的参数：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="meta">@ImportResource</span>(<span class="string">"classpath:/annotations.xml"</span>)</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">VehicleFactoryConfig</span> </span>&#123;&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="3-4-PropertySource"><a href="#3-4-PropertySource" class="headerlink" title="3.4. @PropertySource"></a>3.4. <code>@PropertySource</code></h2><p>通过这个注解，我们可以为应用程序定义属性文件：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="meta">@PropertySource</span>(<span class="string">"classpath:/annotations.properties"</span>)</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">VehicleFactoryConfig</span> </span>&#123;&#125;</span><br></pre></td></tr></table></figure></p>
<p><code>@PropertySource</code>利用Java 8的repeating annotations功能，这意味着我们可以使用多种手段标记一个类：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="meta">@PropertySource</span>(<span class="string">"classpath:/annotations.properties"</span>)</span><br><span class="line"><span class="meta">@PropertySource</span>(<span class="string">"classpath:/vehicle-factory.properties"</span>)</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">VehicleFactoryConfig</span> </span>&#123;&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="3-5-PropertySources"><a href="#3-5-PropertySources" class="headerlink" title="3.5. @PropertySources"></a>3.5. <code>@PropertySources</code></h2><p>我们可以使用这个注解来指定多个<code>@PropertySource</code>配置：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="meta">@PropertySources</span>(&#123; </span><br><span class="line">    <span class="meta">@PropertySource</span>(<span class="string">"classpath:/annotations.properties"</span>),</span><br><span class="line">    <span class="meta">@PropertySource</span>(<span class="string">"classpath:/vehicle-factory.properties"</span>)</span><br><span class="line">&#125;)</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">VehicleFactoryConfig</span> </span>&#123;&#125;</span><br></pre></td></tr></table></figure></p>
<p>注意，从Java 8开始，我们可以通过repeating annotations功能实现上述功能。</p>
<h1 id="4-总结"><a href="#4-总结" class="headerlink" title="4. 总结"></a>4. 总结</h1><p>在本文中，我们看到了最常见的Spring核心注解概述。 我们看到了如何配置bean注入和应用程序上下文，以及如何为组件扫描标记类。这些例子都可以在我们的<a href="https://github.com/eugenp/tutorials/tree/master/spring-mvc-java" target="_blank" rel="noopener">GitHub</a>上获得。</p>

      
    </div>
    <footer class="entry-meta entry-footer">
      
	<span class="ico-folder"></span>
    <a class="article-category-link" href="/categories/Java/">Java</a>

      
  <span class="ico-tags"></span>
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Spring-Annotations/">Spring Annotations</a></li></ul>

      
        <div id="donation_div"></div>

<script src="/js/vdonate.js"></script>
<script>
var a = new Donate({
  title: '如果觉得我的文章对您有用，请随意打赏。您的支持将鼓励我继续创作!', // 可选参数，打赏标题
  btnText: 'Donate', // 可选参数，打赏按钮文字
  el: document.getElementById('donation_div'),
  wechatImage: '/css/images/wechatPay.png',
  alipayImage: '/css/images/alipay.jpg'
});
</script>
      

      
        
	<section id="comments" class="comment">
	  <div id="disqus_thread">
	  <noscript>Please enable JavaScript to view the <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
	  </div>
	</section>

	<script type="text/javascript">
	var disqus_shortname = 'haijdblog';
	(function(){
	  var dsq = document.createElement('script');
	  dsq.type = 'text/javascript';
	  dsq.async = true;
	  dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
	  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
	}());
	(function(){
	  var dsq = document.createElement('script');
	  dsq.type = 'text/javascript';
	  dsq.async = true;
	  dsq.src = '//' + disqus_shortname + '.disqus.com/count.js';
	  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
	}());
	</script>


      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2018/06/13/Spring-Web-Annotations-1/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          Spring Web Annotations
        
      </div>
    </a>
  
  
    <a href="/2018/05/30/Spring-AOP-Minimalist-Getting-Started/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Spring-AOP极简入门</div>
    </a>
  
</nav>

  
</article>

<!-- Table of Contents -->

  <aside id="sidebar">
    <div id="toc" class="toc-article">
    <strong class="toc-title">Contents</strong>
    
      <ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#1-概览"><span class="nav-number">1.</span> <span class="nav-text">1. 概览</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#2-DI有关的注解"><span class="nav-number">2.</span> <span class="nav-text">2. DI有关的注解</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#2-1-Autowired"><span class="nav-number">2.1.</span> <span class="nav-text">2.1. @Autowired</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-2-Bean"><span class="nav-number">2.2.</span> <span class="nav-text">2.2. @Bean</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-3-Qualifier"><span class="nav-number">2.3.</span> <span class="nav-text">2.3. @Qualifier</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-4-Required"><span class="nav-number">2.4.</span> <span class="nav-text">2.4. @Required</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-5-Value"><span class="nav-number">2.5.</span> <span class="nav-text">2.5. @Value</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-6-DependsOn"><span class="nav-number">2.6.</span> <span class="nav-text">2.6. @DependsOn</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-7-Lazy"><span class="nav-number">2.7.</span> <span class="nav-text">2.7. @Lazy</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-8-Lookup"><span class="nav-number">2.8.</span> <span class="nav-text">2.8. @Lookup</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-9-Primary"><span class="nav-number">2.9.</span> <span class="nav-text">2.9. @Primary</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-10-Scope"><span class="nav-number">2.10.</span> <span class="nav-text">2.10. @Scope</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#3-上下文配置注解"><span class="nav-number">3.</span> <span class="nav-text">3. 上下文配置注解</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#3-1-Profile"><span class="nav-number">3.1.</span> <span class="nav-text">3.1. @Profile</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-2-Import"><span class="nav-number">3.2.</span> <span class="nav-text">3.2. @Import</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-3-ImportResource"><span class="nav-number">3.3.</span> <span class="nav-text">3.3. @ImportResource</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-4-PropertySource"><span class="nav-number">3.4.</span> <span class="nav-text">3.4. @PropertySource</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-5-PropertySources"><span class="nav-number">3.5.</span> <span class="nav-text">3.5. @PropertySources</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#4-总结"><span class="nav-number">4.</span> <span class="nav-text">4. 总结</span></a></li></ol>
    
    </div>
  </aside>
</section>
        
      </div>
      <footer id="footer" class="site-footer">
  

  <div class="clearfix container">
      <div class="site-info">
	      &copy; 2018 haijd All Rights Reserved.
          
            <span id="busuanzi_container_site_uv">
              本站访客数<span id="busuanzi_value_site_uv"></span>人次  
              本站总访问量<span id="busuanzi_value_site_pv"></span>次
            </span>
          
      </div>
      <div class="site-credit">
        Theme by <a href="https://github.com/iTimeTraveler/hexo-theme-hiero" target="_blank">hiero</a>
      </div>
  </div>
</footer>


<!-- min height -->

<script>
    var contentdiv = document.getElementById("content");

    contentdiv.style.minHeight = document.body.offsetHeight - document.getElementById("allheader").offsetHeight - document.getElementById("footer").offsetHeight + "px";
</script>
    </div>
    <!-- <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
    <a href="/categories" class="mobile-nav-link">Categories</a>
  
    <a href="/tags" class="mobile-nav-link">Tags</a>
  
    <a href="/about" class="mobile-nav-link">About</a>
  
</nav> -->
    

<!-- mathjax config similar to math.stackexchange -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      processEscapes: true
    }
  });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/scripts.js"></script>
<script src="/js/bootstrap.js"></script>
<script src="/js/main.js"></script>








	<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
	</script>






  </div>

  <a id="rocket" href="#top" class=""></a>
  <script type="text/javascript" src="/js/totop.js" async=""></script>
</body>
</html>
